/**************************************************************
 *@ProjectName: blweibo
 *@FileName: UserFansItemView.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.text.Spannable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.TopicUtil;

/**
 * The Class UserFansItemView.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class UserFansItemView extends LinearLayout implements OnClickListener {

    /** The iv portrait. */
    private ImageView ivPortrait;

    /** The tv name. */
    private TextView tvName;

    /** The tv num. */
    private TextView tvNum;

    /** The tv content. */
    private TextView tvContent;

    /** The tv summary. */
    private TextView tvSummary;

    /** The m user. */
    private User mUser;

    /** The bm. */
    private Bitmap bm;

    /** The task. */
    private AsyncTask task;

    /** The task2. */
    private AsyncTask task2;

    /** The iv attend btn. */
    private Button ivAttendBtn;

    /**
     * Instantiates a new user fans item view.
     * 
     * @param context
     *            the context
     * @param user
     *            the user
     */
    public UserFansItemView(Context context, User user) {
        super(context);
        mUser = user;
        ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(R.layout.fanitemview, this);
        ivPortrait = (ImageView) findViewById(R.id.ivItemPortrait);
        tvName = (TextView) findViewById(R.id.tvItemName);
        tvNum = (TextView) findViewById(R.id.tvItemNum);
        tvContent = (TextView) findViewById(R.id.tvItemContent);
        tvSummary = (TextView) findViewById(R.id.tvItemSummary);
        ivAttendBtn = (Button) findViewById(R.id.ivAttendBtn);
        ivAttendBtn.setOnClickListener(this);
        ivAttendBtn.setVisibility(View.GONE);
        setUI();
    }

    /**
     * Sets the ui.
     */
    private void setUI() {
        if (task != null && task.getStatus() == Status.RUNNING) {
            task.cancel(true);
        }
        if (task2 != null && task2.getStatus() == Status.RUNNING) {
            task2.cancel(true);
        }
        tvName.setText(mUser.getUsername());
        tvNum.setText(mUser.getFans_count());
        tvContent.setText(mUser.getAboutme());
        String str = mUser.getTopic().getText();
        if ((str != null) && (str.length() > 35)) {
            str = str.substring(0, 35) + "...";
        }
        tvSummary.setText(str, TextView.BufferType.SPANNABLE);
        TopicUtil.highlightContent(getContext(), (Spannable) tvSummary.getText(), false);
        task = new ImageTask().execute(0);
        task2 = new FriendTask().execute(0);
    }

    /**
     * Show portrait.
     */
    private void showPortrait() {
        ivPortrait.setImageBitmap(bm);
    }

    /**
     * Update.
     * 
     * @param context
     *            the context
     * @param user
     *            the user
     */
    public void update(Context context, User user) {
        mUser = user;
        ivPortrait.setImageResource(R.drawable.portrait);
        ivAttendBtn.setVisibility(View.GONE);
        setUI();
    }

    /**
     * The Class ImageTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class ImageTask extends AsyncTask<Object, Integer, Object> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            bm = HttpUtil.getImage(getContext(), mUser.getFace());
            return bm;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            showPortrait();
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        new AttentionTask().execute(0);
    }

    /**
     * The Class FriendTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FriendTask extends AsyncTask<Object, Integer, JSONObject> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected JSONObject doInBackground(Object... paramArrayOfString) {
            return HttpUtil.isFriend(getContext(), mUser.getUid());
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(JSONObject result) {
            if (result == null || result.isNull("source")) {
                return;
            }
            boolean b1 = result.optJSONObject("source").optBoolean("following");
            if (!b1) {
                ivAttendBtn.setText(R.string.attend);
                ivAttendBtn.setBackgroundResource(R.drawable.attend_do);
            } else {
                ivAttendBtn.setText(R.string.user_delattention);
                ivAttendBtn.setBackgroundResource(R.drawable.attend_cancel);
            }
            ivAttendBtn.setVisibility(View.VISIBLE);
        }
    }

    /**
     * The Class AttentionTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class AttentionTask extends AsyncTask<Object, Integer, Object> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            if (ivAttendBtn.getText().toString().equals(getContext().getString(R.string.user_delattention))) {
                mUser = HttpUtil.getUserInfo(getContext(), Constant.FRIENDSHIPS_DESTROY + mUser.getUid() + ".json");
            } else {
                mUser = HttpUtil.getUserInfo(getContext(), Constant.CREATE + mUser.getUid() + ".json");
            }
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            setUI();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(getContext());
            pd.setMessage("请稍候……");
            pd.show();
        }

    }

}
